from flask import Flask,jsonify,request
import json
from flask.config import Config
from werkzeug import datastructures
app = Flask(__name__)
from db import db1
from config import Config
from flask_cors import CORS
from views.course import bp_course
app.config.from_object(Config)
CORS(app)
app.register_blueprint(bp_course)



@app.route('/test',methods=['POST','GET'])
def test():
    if request.method=='POST':
        data =request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "insert into test(name) values('%s')"%(data['name'])
        db1.update(sql)
        return jsonify({'code':200})

    elif request.method=='GET':
        sql = "select * from test"
        res = db1.findall(sql)
        return jsonify({'code':200,'list':res})

@app.route('/paramsM',methods=['POST','GET'])
def paramsM():
    if request.method=='POST':
        data =request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        sql="update test set params='%s' where id=%d"%(data['params'],int(data['id']))
        print(sql)
        db1.update(sql)
        return jsonify({'code':200})
    else:
        id = request.args.get('wid')
        sql = "select * from test where id=%d"%(int(id))
        res = db1.find(sql)
        return jsonify({'code':200,'params':res})
        

if __name__ == '__main__':
    app.run()
    